package com.wq.httpclientcore.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wq.httpclientcore.convert.Convertor;
import com.wq.httpclientcore.entities.po.DbConnectionConfig;
import com.wq.httpclientcore.entities.req.save.DbConnectionConfigSaveREQ;
import com.wq.httpclientcore.entities.req.query.DbConnectionConfigREQ;
import com.wq.httpclientcore.entities.vo.DbConnectionConfigVO;
import com.wq.httpclientcore.model.Result;
import com.wq.httpclientcore.service.DbConnectionConfigService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * @author wangqi
 * @since 2025-03-12 14:31:39
 */
@RequiredArgsConstructor
@RestController
@RequestMapping("/v1/module/dbConnectionConfig")
@Tag(name = "数据库连接配置管理", description = "提供数据库连接配置的增删改查及分页查询功能")
public class DbConnectionConfigController {

    private final DbConnectionConfigService dbConnectionConfigService;

    @GetMapping("/list")
    @Parameters({@Parameter(name = "pageNo", hidden = true), @Parameter(name = "pageSize", hidden = true)})
    @Operation(summary = "获取数据库连接配置列表", description = "根据条件查询数据库连接配置列表")
    @ApiResponse(responseCode = "200", description = "成功获取数据库连接配置列表", content = @Content(schema = @Schema(implementation = Result.class)))
    public Result<List<DbConnectionConfigVO>> list(@Parameter(description = "数据库连接配置查询请求参数", required = false) DbConnectionConfigREQ req) {
        List<DbConnectionConfig> poList = dbConnectionConfigService.list(req);
        return Result.success(Convertor.INSTANCE.convertDbConnectionConfigs(poList));
    }

    @GetMapping("/page")
    @Operation(summary = "分页获取数据库连接配置列表", description = "根据条件分页查询数据库连接配置列表")
    @ApiResponse(responseCode = "200", description = "成功分页获取数据库连接配置列表", content = @Content(schema = @Schema(implementation = Result.class)))
    public Result<IPage<DbConnectionConfigVO>> page(@Parameter(description = "数据库连接配置查询请求参数，包含分页信息", required = false) DbConnectionConfigREQ req) {
        Page<DbConnectionConfig> page = dbConnectionConfigService.pageList(req);
        IPage<DbConnectionConfigVO> result = page.convert(Convertor.INSTANCE::convert);
        return Result.success(result);
    }

    @GetMapping("/{id}")
    @Operation(summary = "根据 ID 获取数据库连接配置", description = "根据指定的 ID 查询数据库连接配置信息")
    @ApiResponse(responseCode = "200", description = "成功获取数据库连接配置信息", content = @Content(schema = @Schema(implementation = Result.class)))
    @ApiResponse(responseCode = "404", description = "数据不存在或已删除", content = @Content(schema = @Schema(implementation = Result.class)))
    public Result<DbConnectionConfigVO> getById(@Parameter(description = "数据库连接配置的 ID", required = true) @PathVariable("id") Long id) {
        DbConnectionConfig data = dbConnectionConfigService.getById(id);
        if (null == data) {
            return Result.fail("数据不存在或已删除");
        }
        return Result.success(Convertor.INSTANCE.convert(data));
    }

    @PostMapping
    @Operation(summary = "新增数据库连接配置", description = "新增一条数据库连接配置信息")
    @ApiResponse(responseCode = "200", description = "成功新增数据库连接配置", content = @Content(schema = @Schema(implementation = Result.class)))
    public Result<?> add(@Parameter(description = "数据库连接配置保存请求参数", required = true) @RequestBody DbConnectionConfigSaveREQ req) {
        DbConnectionConfig po = Convertor.INSTANCE.convert(req);
        return Result.success(dbConnectionConfigService.save(po));
    }

    @PutMapping("/{id}")
    @Operation(summary = "编辑数据库连接配置", description = "根据指定的 ID 编辑数据库连接配置信息")
    @ApiResponse(responseCode = "200", description = "成功编辑数据库连接配置", content = @Content(schema = @Schema(implementation = Result.class)))
    public Result<?> edit(@Parameter(description = "数据库连接配置的 ID", required = true) @PathVariable("id") Long id, @Parameter(description = "数据库连接配置保存请求参数", required = true) @RequestBody DbConnectionConfigSaveREQ req) {
        DbConnectionConfig po = Convertor.INSTANCE.convert(req);
        po.setId(id);
        return Result.success(dbConnectionConfigService.updateById(po));
    }

    @DeleteMapping("/{ids}")
    @Operation(summary = "删除数据库连接配置", description = "根据指定的 ID 数组删除数据库连接配置信息")
    @ApiResponse(responseCode = "200", description = "成功删除数据库连接配置", content = @Content(schema = @Schema(implementation = Result.class)))
    public Result<?> delete(@Parameter(description = "数据库连接配置的 ID 数组", required = true) @PathVariable Long[] ids) {
        dbConnectionConfigService.deletedById(Arrays.asList(ids));
        return Result.success("删除成功");
    }
}